﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace DarkLakeTools.CSSUtility
{
    class CompressCSS
    {
        public static string Compress(string bodyOriginal)
        {
            bodyOriginal = Regex.Replace(bodyOriginal, "/\\*.+?\\*/", "", RegexOptions.Singleline); 

            StringBuilder body = new StringBuilder(bodyOriginal); 
                        
            body.Replace("  ", string.Empty);
            body.Replace(Environment.NewLine + Environment.NewLine + Environment.NewLine, string.Empty);
            body.Replace(Environment.NewLine + Environment.NewLine, Environment.NewLine);
            body.Replace(Environment.NewLine, string.Empty);
            body.Replace("\\t", string.Empty);
            body.Replace(" {", "{");
            body.Replace(" :", ":");
            body.Replace(": ", ":");
            body.Replace(", ", ",");
            body.Replace("; ", ";");
            body.Replace(";}", "}");

            bodyOriginal = body.ToString(); 
            bodyOriginal = Regex.Replace(bodyOriginal, "/\\*[^\\*]*\\*+([^/\\*]*\\*+)*/", "$1");
            bodyOriginal = Regex.Replace(bodyOriginal, "(?<=[>])\\s{2,}(?=[<])|(?<=[>])\\s{2,}(?=&nbsp;)|(?<=&ndsp;)\\s{2,}(?=[<])", string.Empty);

            return body.ToString(); 
        }

    }
}

